package controller.main
{
	import events.library.EventAddResourcesToLibrary;
	import events.library.EventUpdateLibraryResource;
	import events.main.EventFilesSelect;
	import events.main.EventLoadProject;
	import events.main.EventSaveProject;
	
	import model.ModelBrowseFile;
	import model.ModelLibrary;
	import model.ModelLibraryConflicts;
	import model.ModelProject;
	
	import org.flashIso.engine.library.objects.LBinaryData;
	import org.flashIso.engine.library.objects.LImage;
	import org.robotlegs.mvcs.Command;
	
	import services.IServiceFile;
	
	public class CommandFilesSelected extends Command
	{
		
		[Inject]
		public var mb:ModelBrowseFile;
		
		[Inject]
		public var mp:ModelProject;
		
		[Inject]
		public var e:EventFilesSelect;
		
		[Inject]
		public var sb:IServiceFile;
		
		override public function execute() : void {
			var files:Array = e.files;
			var i:uint;
			var j:uint;
			switch (mb.currentBrowseType) {
				case mb.BROWSE_FOR_UPDATE_IMAGE:
					dispatch(new EventUpdateLibraryResource(EventUpdateLibraryResource.UPDATE_LIBRARY_RESOURCE, mb.imageIdToUpdate, sb.readFile(files[0]), EventUpdateLibraryResource.TYPE_IMAGE));
					break;
				case mb.BROWSE_FOR_UPDATE_BINARY:
					dispatch(new EventUpdateLibraryResource(EventUpdateLibraryResource.UPDATE_LIBRARY_RESOURCE, mb.binaryIdToUpdate, sb.readFile(files[0]), EventUpdateLibraryResource.TYPE_BINARY));
					break;
				case mb.BROWSE_FOR_OPEN_IMAGES:
					dispatch(new EventAddResourcesToLibrary(EventAddResourcesToLibrary.ADD_RESOURCE_TO_LIBRARY, files));
					break;
				case mb.BROWSE_FOR_OPEN_BINARIES:
					dispatch(new EventAddResourcesToLibrary(EventAddResourcesToLibrary.ADD_RESOURCE_TO_LIBRARY, null, files));
					break;
				case mb.BROWSE_FOR_SAVE_PROJECT:
					mp.projectFilePath = files[0];
					mp.projectName = sb.getFileName(mp.projectFilePath);
					dispatch(new EventSaveProject(EventSaveProject.SAVE_PROJECT));
					break;
				case mb.BROWSE_FOR_LOAD_PROJECT:
					dispatch(new EventLoadProject(EventLoadProject.LOAD_PROJECT, files[0]));
					break;
			}
		}
		
	}
}